/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   MainTenanceAreaServiceImpl.java for com.pengtu.gsj.service.impl.check.yh.project    </p>
 * 
 * <p>最后修改 : $: Aug 24, 2017-5:35:07 PM v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.yh.project;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.yh.MainTenanceDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.yh.MainTenance;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.yh.project.MainTenanceAreaService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * MainTenanceAreaServiceImpl
 * 
 * Aug 24, 2017 5:35:07 PM
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class MainTenanceAreaServiceImpl implements MainTenanceAreaService {
	
	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	private MainTenanceDao mainTenanceDao;
	
	@Override
	public MainTenance getMainTenanceById(String id) {
		return mainTenanceDao.get(id);
	}
	
	@Override
	public Map<String, Object> getCheckContent(String startTime,
			String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> result = new ArrayList<Attachment>();
		List<Object> values = new ArrayList<Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		String maintenceId = (String)jsonObject.get("mainTenanceId");
		
		result = systemManageService.getCheckAttachments(startTime, endTime, tag);
		//根据orgId和时间 、标签查询是否有无附件 如果有 再将相关的附件加入到list中去
//		List<Attachment> list = systemManageService.getCheckAttachments(startTime, endTime, tag, maintenceId);
		double total = 0; //检查得分
		double percentage = 0; //系统完善度
		double tableFull = yearFull*4;
		List<String> resultList = Arrays.asList("0", "0", "0");
		if (StringUtils.isNotEmpty(maintenceId)) {
			MainTenance mainTenance = getMainTenanceById(maintenceId);
			if (mainTenance == null) {
				map.put("electronization", false);
			} else {
				for (Attachment attachment : mainTenance.getAttachments()) {
					if ("yhzyq_bzfa".equals(attachment.getTag())) {
						resultList.set(0, "1");
						result.add(attachment);
					}
					if ("yhzyq_bsgf".equals(attachment.getTag())) {
						resultList.set(1, "1");
						result.add(attachment);
					}
				}
				if ("1".equals(resultList.get(0)) && "1".equals(resultList.get(1))) {
					total = MathsUtils.sumDouble(total, yearFull);
				} else if ("1".equals(resultList.get(0)) && "0".equals(resultList.get(1))) {
					total = MathsUtils.sumDouble(total, yearPart);
				}
				total = total < 0 ? 0 : total;
				percentage = MathsUtils.divDouble(total*100, tableFull, 2);
				resultList.set(2, String.valueOf(total));
				map.put("electronization", true);
				map.put("name", mainTenance.getProjectName());
				values.add(maintenceId);
			}
		} else {
			//判断是否有无检查项目
			map.put("electronization", false);
		}
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("att", result);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		map.put("resultList", resultList);
		map.put("percentage", percentage);
		return map;
	}
}
